package com.hejian.android.a72ggametools.activity;

import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hejian.android.a72ggametools.R;
import com.hejian.android.a72ggametools.bean.User;
import com.hejian.android.a72ggametools.common.Constant;
import com.hejian.android.a72ggametools.net.HttpUtils;
import com.hejian.android.a72ggametools.utils.UserInteraction;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/1/18.
 */
public class PhoneNumCheckActivity extends AppCompatActivity implements View.OnClickListener {
    private Button backBtn;
    private Intent dataIntent;
    private String userName;
    private String nickName;
    private String password;
    private TextView userNameTextView;
    private Button checkNumBtn;
    private Button completeBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_check);
        initData();
        initView();
        setListener();

    }

    private void initTimeTask() {
       timeTask = new TimerTask() {
            @Override
            public void run() {
                if (index == 0){

                    index = 120;
                    timer.cancel();
                    mHandler.sendEmptyMessage(2);
                }else {
                    mHandler.sendEmptyMessage(3);
                }



            }
        };
    }

    private void initData() {
        dataIntent = getIntent();
        if (dataIntent.getStringExtra("sign").equals("fromregister")){
            userName = dataIntent.getStringExtra("username");
            nickName = dataIntent.getStringExtra("nickname");
            password = dataIntent.getStringExtra("password");
        }
    }

    private void setListener() {
        backBtn.setOnClickListener(this);
        checkNumBtn.setOnClickListener(this);
        completeBtn.setOnClickListener(this);
    }

    private void initView() {
        backBtn = (Button)findViewById(R.id.phone_check_back);
        userNameTextView = (TextView)findViewById(R.id.phone_check_username);
        userNameTextView.setText(userName);
        checkNumBtn = (Button)findViewById(R.id.phone_check_getnum);
        completeBtn = (Button)findViewById(R.id.phone_check_complete);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.phone_check_back:
                finish();
                break;
            case R.id.phone_check_getnum:
                getCheckNum();
                initTimeTask();
                break;
            case R.id.phone_check_complete:
                UserInteraction userInteraction = new UserInteraction(getApplicationContext());
                User user = new User(userName,password,nickName);
                boolean flag = userInteraction.register(user);
                if (flag) {
                    Intent intent = new Intent(PhoneNumCheckActivity.this, MainActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("username",user.getUserName());
                    bundle.putString("password",user.getPassword());
                    bundle.putString("nickname",user.getNickName());
                    intent.putExtra("userbundle",bundle);
                    intent.putExtra("sign", "fromcheck");
                    intent.putExtra("hasdata",true);
                    startActivity(intent);
                }
                break;
        }
    }

    private void getCheckNum() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                doRequestCheckNum();
            }


        }).start();
    }

    private void doRequestCheckNum() {
        Map<String,String> map = new HashMap<String, String>();
        map.put("op","send");
        map.put("username",userName);
        String buffer = HttpUtils.doPostRequest(Constant.CHECK_NUM_URL,map);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(buffer);
            String msg = jsonObject.getString("msg");
            if (msg.equals("验证码发送成功")){
                mHandler.sendEmptyMessage(1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private Timer timer;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1){
                timer = new Timer(true);
                timer.schedule(timeTask,0,1000);
            }
            if (msg.what == 2){
                checkNumBtn.setText("重新获取验证码");
                timeTask = null;
            }
            if (msg.what ==3){
                checkNumBtn.setText(String.valueOf(index));
                index -- ;
            }

        }
    };

    private int index = 120;
    private TimerTask timeTask = null;

    @Override
    protected void onStop() {
        timeTask = null;
        super.onStop();
    }
}
